package org.springframework.sandbox.nettymvcpost.nettymvc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
//import com.itjoin.pro_netty.annotation.Remote;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;

@Controller
public class NettyUserPostController {
    //此方法用于远程调用，需要加上Remote注解
    //@RequestMapping(value="/getUserNameByIdextPost",method= RequestMethod.POST,produces = "text/html; charset=utf-8")
    @RequestMapping(value="/getUserNameByIdextPost",method= RequestMethod.POST,produces = "application/json; charset=utf-8")
    //@Remote("getUserNameById")
    //直接转Json，可能是因为SpringMVC的配置文件少Json的jar，所以没有成功。临时采用HttpServletRequest的方式，将其转为类进行处理。
    public @ResponseBody
    String getUserNameById(String userId,@RequestBody RequestFutureVO requestFutureVO, HttpServletRequest request, HttpServletResponse response){
        //此处并未访问数据库，只是做简单输出
        System.out.println("客户端请求的用户id为======"+userId);
        System.out.println(requestFutureVO);
        /*String param= null;
        try {
            BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                responseStrBuilder.append(inputStr);

            JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString());
            param= jsonObject.toJSONString();
            RequestFutureVO futureVO= JSON.parseObject(param,requestFutureVO.getClass());
            System.out.println(futureVO);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        //return param;
        return "响应结果===用户张三"+userId;
    }
}
